<!--
 * @Author: 白川 杏 baiyufeisteam@163.com
 * @Date: 2023-04-19 08:23:23
 * @LastEditors: 白川 杏 baiyufeisteam@163.com
 * @LastEditTime: 2023-06-05 15:55:58
 * @FilePath: \backend\vueproject\src\components\User.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
    <div>
        <div>
            <el-divider content-position="left">您具有一定的操作权限</el-divider>
        </div>

        <div>
            <el-divider>
                <span id="spanlittlepart">查询</span>
            </el-divider>
        </div>

        <!-- 查询 -->
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="用户名" style="width: 200px;">
                <el-input v-model="checkformInline.userinfo" placeholder="userinfo"></el-input>
            </el-form-item>
            <el-form-item label="签到时间" style="width: 220px;">
                <el-input v-model="checkformInline.date" placeholder="格式如‘2023-5-14’"></el-input>
            </el-form-item>
            <el-form-item style="width: 200px;">
                <el-button @click="search" type="primary" plain style="width: 200px;">查询</el-button>
            </el-form-item>
        </el-form>

        <el-divider>
            <el-icon><star-filled /></el-icon>
        </el-divider>

        <!-- 展示表格 -->
        <el-table :data="tableData" style="width: 100%" highlight-current-row="true" :scrollbar-always-on="true">
            <el-table-column prop="userid" label="用户id" width="" />
            <el-table-column prop="userinfo" label="用户名" width="" />
            <el-table-column prop="addcoin" label="签到可获硬币数" width="" />
            <el-table-column prop="date" :formatter="Formatter" label="签到时间" width="" />
            <el-table-column prop="sumcoin" label="总硬币数" width="" />
            <el-table-column prop="type" label="类型" width="" />
        </el-table>

        <el-pagination background layout="prev, pager, next" :current-page="page" @update:current-page="handleCurrentChange"
            :total="total" :page-size="size" style="bottom: 0; text-align: center;margin-left: 45%;margin-top: 5px;" />
    </div>
</template>
<script setup>
import {
    Guide,
    PictureFilled,
    User,
    HomeFilled,
    ChatLineSquare,
    Apple,
    Document,
    List,
    Tools,
    ShoppingCart,
    CollectionTag,
    Finished,
    InfoFilled,
    Cherry,
    Goods,
    Sell,
    ShoppingCartFull,
    Van,
    Sunrise,
    StarFilled,
    EditPen,
    Edit,
    Promotion,
    Grape,
    Pear,
} from '@element-plus/icons-vue';
import dayjs from 'dayjs';
import { ElMessage } from 'element-plus';
import axios from 'axios';
const formLabelWidth = '140px';

</script>
<script>

export default {
    data() {
        return {
            baseURL: 'http://localhost:8080',
            coinChosen: '签到增加/累计签到增加',
            coinOptions: [
                {
                    value: '签到增加/累计签到增加',
                    label: '签到增加/累计签到增加',
                },
                {
                    value: '签到增加',
                    label: '签到增加',
                },
                {
                    value: '累计签到增加',
                    label: '累计签到增加',
                },
            ],
            formInline: {
                userid: 0,
                addcoin: 0,
                date: '格式如‘2023-5-14’',
                sumcoin: 0,
                coinid: 0,
                type: 999,
            },
            checkformInline: {
                userid: '',
                addcoin: 0,
                date: '',
                sumcoin: 0,
                coinid: 0,
                type: 999,
                userinfo: '',
            },
            dialogFormVisible: false,
            dialogaddFormVisible: false,
            showSearch: false,
            tableData: [],
            tableDataStore: [],
            tableimgList: [],
            tableimg: '',
            coin: {
                uscoinrid: 0,
                addcoin: 0,
                date: 0,
                sumcoin: 0,
                coinid: 0,
                type: 999,
            },
            form: {
                userid: 0,
                addcoin: 0,
                date: 0,
                sumcoin: 0,
                coinid: 0,
                type: 999,
            },
            searchForm: [],
            searchtime: [],
            year: 0,
            month: 0,
            daynum: 0,
            page: 1, // 当前页码
            total: 0, // 总条数
            size: 10, // 每页的数据条数
            reallyTable: [],
            selData: [],
        }
    },
    methods: {
        search() {
            let arr = []
            console.log(this.tableDataStore.length)
            for (let i = 0; i < this.tableDataStore.length; i++) {
                this.tableDataStore[i].date = this.formatDate(this.tableDataStore[i].date);
            }
            for (let i = 0; i < this.tableDataStore.length; i++) {
                if (String(this.tableDataStore[i].userinfo).includes(this.checkformInline.userinfo) &&
                    String(this.tableDataStore[i].date).includes(this.checkformInline.date)) {
                    arr.push(this.tableDataStore[i])
                }
            }
            console.log(arr);
            this.tableData = arr.slice(
                (this.page - 1) * this.size,
                this.page * this.size
            );
            this.selData = arr;
            this.total = arr.length;
        },
        formatDate(date) {
            var y = new Date(date).getFullYear();
            var m = new Date(date).getMonth() + 1;
            m = m < 10 ? '0' + m : m;
            var d = new Date(date).getDate();
            d = d < 10 ? ('0' + d) : d;
            return y + '-' + m + '-' + d;
        },
        Formatter(row, column) {
            if (row.date) {
                console.log(new Date(row.date));
                this.day = new Date(row.date);
            }
            //console.log(this.formatDate(this.day));
            return this.formatDate(row.date);
        },
        backtosimpleformatDate(date) {
            var y = new Date(date).getFullYear();
            this.year = y;
            var m = new Date(date).getMonth() + 1;
            m = m < 10 ? '0' + m : m;
            this.month = m;
            var d = new Date(date).getDate();
            d = d < 10 ? ('0' + d) : d;
            this.daynum = d;
            return y + '-' + m + '-' + d;
        },
        backtosimpleTime(date) {
            //注意这里的参数需要改一下
            this.searchtime = this.checkformInline.date.split('-');
            this.backtosimpleformatDate(date);
            if (this.searchtime[0] == this.year && this.searchtime[1] == this.month && this.searchtime[2] == this.daynum) {
                return 'get';
            }
            else {
                return 'fail';
            }
        },
        getType(index) {
            return this.tableData[index].type == 0 ? this.tableData[index].type = '签到增加' : this.tableData[index].type = '累计签到增加'
        },
        async handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.page = val
            this.tableData = this.selData.slice(
                (this.page - 1) * this.size,
                this.page * this.size
            );
            // this.currentPage = val;
            const data = { 'page': val }
            // await this.onload(data)
        },
    },
    mounted() {
        //页面加载完成后，将数据库内的内容赋给定义的data变量
        axios({
            method: 'post',
            url: 'http://localhost:8080/coin/getcoinbackend',
            withCredentials: true,
        }).then(res => {
            this.tableData = res.data.data;
            this.tableDataStore = res.data.data;
            console.log(res.data.data);
            this.tableData = res.data.data.slice(
                (this.page - 1) * this.size,
                this.page * this.size
            );
            this.total = res.data.data.length;
            this.selData = res.data.data;
            for (var i = 0; i < res.data.data.length; i++) {
                this.getType(i);
            }
        })
    }
}
</script>
<style scoped>
#sidepart {
    width: 100%;
    position: absolute;
    top: 5px;
    font-family: '华文中宋';
}

#mainpart {
    width: 100%;
    position: absolute;
    top: 20px;
    /* left: 260px; */
    font-family: '华文中宋';
}

#spanpart {
    font-size: 30px;
    font-family: '华文中宋';
}

/* dialog */
.el-button--text {
    margin-right: 15px;
}

.el-select {
    width: 300px;
}

.el-input {
    width: 300px;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}

#spanlittlepart {
    font-size: 15px;
    color: cadetblue;
    font-family: '华文中宋';
}
</style>